﻿using Infrastructure;
using Inspection.Helper;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Markup;

namespace Inspection.Models
{
    /// <summary>
    /// 系统初始化参数
    /// </summary>
    [Serializable]
    public class SysInitialParame : PropertyChangedNotify
    {
        /// <summary>
        /// 主控的TCP端口
        /// </summary>
        public int AlgTcpPortNum
        {
            get { return _algTcpPortNum; }
            set { _algTcpPortNum = value; OnPropertyChanged(); }
        }
        private int _algTcpPortNum = 8000;

        /// <summary>
        /// 主控的IP
        /// </summary>
        public string MainSWIP
        {
            get { return mainSWIP; }
            set
            {
                if (value != null && value.IsIpAdress())
                {
                    mainSWIP = value;
                    OnPropertyChanged();
                }
            }
        }
        private string mainSWIP = "127.0.0.1";

        /// <summary>
        /// 项目文件路径
        /// </summary>
        public string ProjectFile
        {
            get { return _rojectFile; }
            set
            {
                _rojectFile = value;
                OnPropertyChanged();
            }
        }
        private string _rojectFile = string.Empty;

        /// <summary>
        /// 监听的exe名称
        /// </summary>
        public string WatchExeName
        {
            get { return _watchExeName; }
            set
            {
                _watchExeName = value;
                OnPropertyChanged();
            }
        }
        private string _watchExeName = string.Empty;

        /// <summary>
        /// 工位号
        /// </summary>
        public int SlotNum
        {
            get { return _slotNum; }
            set
            {
                _slotNum = value;
                OnPropertyChanged();
            }
        }
        private int _slotNum = 0;

        public void Save()
        {
            string dir = AppDomain.CurrentDomain.BaseDirectory + "Config";
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
            XmlSerializerHelper.Save(this, FilePath);
        }

        public static SysInitialParame Load()
        {
            if (!System.IO.File.Exists(FilePath)) return new SysInitialParame();
            string strErr = string.Empty;
            SysInitialParame system = XmlSerializerHelper.Load<SysInitialParame>(FilePath, ref strErr, false);
            return system;
        }

        private static string FilePath = AppDomain.CurrentDomain.BaseDirectory + "Config\\Sys.xml";
    }

    [MarkupExtensionReturnType(typeof(Func<bool>))]
    public class StaticMethodExtension : MarkupExtension
    {
        public StaticMethodExtension(string method)
        {
            Method = method;
        }
        [ConstructorArgument("method")]
        public string Method { get; set; }

        private Func<bool> _func;

        #region Overrides of MarkupExtension

        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (_func == null)
            {
                int index = Method.IndexOf('.');
                if (index < 0)
                {
                    throw new ArgumentException("MarkupExtensionBadStatic");
                }
                string qualifiedTypeName = this.Method.Substring(0, index);
                if (qualifiedTypeName == string.Empty)
                {
                    throw new ArgumentException("MarkupExtensionBadStatic");
                }
                IXamlTypeResolver service = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver;
                if (service == null)
                {
                    throw new ArgumentException("MarkupExtensionNoContext");
                }
                var memberType = service.Resolve(qualifiedTypeName);
                var str = this.Method.Substring(index + 1, (this.Method.Length - index) - 1);

                if (str == string.Empty)
                {
                    throw new ArgumentException("MarkupExtensionBadStatic");
                }

                var reflectedFunc = memberType.GetMethod(str,
                                                         BindingFlags.FlattenHierarchy | BindingFlags.Public |
                                                         BindingFlags.Static);

                if (reflectedFunc != null)
                {
                    if (reflectedFunc.ReturnType == typeof(bool))
                    {
                        var v = Delegate.CreateDelegate(typeof(Func<bool>), reflectedFunc, true);

                        _func = (Func<bool>)v;
                    }

                }

            }

            return _func;
        }

        #endregion
    }
}
